package com.example.java_chatroom.config;

import com.example.java_chatroom.controller.TextWebSocketController;
import com.example.java_chatroom.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private TextWebSocketController textWebSocketController;

    @Autowired
    private WebSocketController webSocketController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //registry.addHandler(textWebSocketController,"/test");
        registry.addHandler(webSocketController,"/WebSocketMessage")
                //通过这个拦截器，可以将HttpSession中存的键值对复制一份到WebSocketSession中去
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
